package com.platform.controller;

import com.platform.entity.RemitEntity;
import com.platform.entity.UserEntity;
import com.platform.service.RemitService;
import com.platform.service.UserService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author zzh
 * @email 862583291@qq.com
 * @date 2018-11-13 15:02:28
 */
@RestController
@RequestMapping("remit")
public class RemitController {
    @Autowired
    private RemitService remitService;
    @Autowired
    private UserService userService;
    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("remit:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        params.put("remitStatus", "2");
        Query query = new Query(params);
        List<RemitEntity> remitList = remitService.queryList(query);
        int total = remitService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(remitList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }
    /**
     * 统计列表
     */
    @RequestMapping("/lists")
    @RequiresPermissions("remit:list")
    public R lists(@RequestParam Map<String, Object> params) {
        params.put("sidx", "cycle");
        params.put("order", "desc");
        params.put("groupBy", "cycle,nideshop_mobile");
        params.put("remitStatus", "2");
        if(params.get("period")!=null){
            if(params.get("period").toString().equals("2")){
                params.put("fields", "a.id,a.nideshop_user,WEEK(a.apply_time) cycle,sum(a.remit_fee) remit_fee,d.realname nideshop_name,d.mobile nideshop_mobile");
            }else if(params.get("period").toString().equals("3")){
                params.put("fields", "a.id,a.nideshop_user,MONTH(a.apply_time) cycle,sum(a.remit_fee) remit_fee,d.realname nideshop_name,d.mobile nideshop_mobile");
            }else if(params.get("period").toString().equals("4")){
                params.put("fields", "a.id,a.nideshop_user,QUARTER(a.apply_time) cycle,sum(a.remit_fee) remit_fee,d.realname nideshop_name,d.mobile nideshop_mobile");
            }else if(params.get("period").toString().equals("5")){
                params.put("fields", "a.id,a.nideshop_user,YEAR(a.apply_time) cycle,sum(a.remit_fee) remit_fee,d.realname nideshop_name,d.mobile nideshop_mobile");
            }else if(params.get("period").toString().equals("6")){
                params.put("sidx", "id");
                params.put("order", "desc");
                params.put("groupBy", "nideshop_mobile");
                params.put("fields", "a.id,a.nideshop_user,sum(a.remit_fee) remit_fee,d.realname nideshop_name,d.mobile nideshop_mobile");
            }else{
                params.put("fields","a.id,a.nideshop_user,DATE(a.apply_time) cycle,sum(a.remit_fee) remit_fee,d.realname nideshop_name,d.mobile nideshop_mobile");
            }
        }else{
            params.put("fields","a.id,a.nideshop_user,DATE(a.apply_time) cycle,sum(a.remit_fee) remit_fee,d.realname nideshop_name,d.mobile nideshop_mobile");
        }
        //查询列表数据
        Query query = new Query(params);
        List<RemitEntity> remitList = remitService.queryList(query);
        int total = remitService.queryTotal2(query);
        PageUtils pageUtil = new PageUtils(remitList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看购物劵兑换申请列表
     */
    @RequestMapping("/conversionlist")
    @RequiresPermissions("remit:list")
    public R conversionlist(@RequestParam Map<String, Object> params) {
        params.put("sidx", "a.apply_time");
        params.put("order", "desc");
        params.put("conversion", "true");
        params.put("fields","a.*,b.realname sys_name,b.mobile sys_mobile,d.mobile nideshop_mobile,d.realname nideshop_name,e.bank_name,e.opening_bank,e.account,e.user_name,e.card_type_name");
        Query query = new Query(params);
        List<RemitEntity> remitList = remitService.queryList(query);
        int total = remitService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(remitList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("remit:info")
    public R info(@PathVariable("id") Integer id) {
        RemitEntity remit = remitService.queryObject(id);
        return R.ok().put("remit", remit);
    }

    /**·hul
     *
     * 转账完成操作
     */
    @RequestMapping("/remitted")
    @RequiresPermissions("remit:update")
    @Transactional
    public R remitted(@RequestBody RemitEntity remit) {
        if(StringUtils.isNullOrEmpty(remit.getId())){ return R.error("请求参数不能为空"); }
        RemitEntity remitEntity = remitService.queryObject(remit.getId());
        if(remitEntity==null || remitEntity.getRemitStatus()==2 || remitEntity.getRemitStatus()==3){
            return R.error("不允许操作");
        }
        if(remitEntity.getSysUser()==1){
            UserEntity longinUser = userService.queryObject(remitEntity.getNideshopUser().intValue());
            UserEntity xiaoxiaoUser = userService.queryObject(1);
            if(remitEntity.getRemitType()==1){
                return R.error("数据异常");
            }
            if(remitEntity.getRemitType()==2){
                if(StringUtils.isNullOrEmpty(xiaoxiaoUser.getBalanceFee()) || (xiaoxiaoUser.getBalanceFee().compareTo(remitEntity.getRemitFee())<0)){
                    return R.error("账户金额不足");
                }
                xiaoxiaoUser.setBalanceFee(xiaoxiaoUser.getBalanceFee().subtract(remitEntity.getRemitFee()));
                if(StringUtils.isNullOrEmpty(longinUser.getBalanceFee())){
                    longinUser.setBalanceFee(remitEntity.getRemitFee());
                }else{
                    longinUser.setBalanceFee(longinUser.getBalanceFee().add(remitEntity.getRemitFee()));
                }
            }
            userService.update(longinUser);
            userService.update(xiaoxiaoUser);
        }
        remitEntity.setRemitStatus(2);
        remitEntity.setRemitTime(new Date());
        remitEntity.setApplyTime(null);
        int remitIndex = remitService.update(remitEntity);
        if(remitIndex!=1){
            R.error("操作失败");
        }
        return R.ok("执行成功");
    }

    /**
     * 转账失败操作
     */
    @RequestMapping("/defeated")
    @RequiresPermissions("remit:update")
    @Transactional
    public R defeated(@RequestBody RemitEntity remit) {
        if(StringUtils.isNullOrEmpty(remit.getId())){ return R.error("请求参数不能为空"); }
        RemitEntity remitEntity = remitService.queryObject(remit.getId());
        if(remitEntity==null || remitEntity.getRemitStatus()==2 || remitEntity.getRemitStatus()==3){
            return R.error("不允许操作");
        }
        if(remitEntity.getNideshopUser()==1){
            UserEntity sysUser = userService.queryObject(remitEntity.getSysUser().intValue());
            UserEntity xiaoxiaoUser = userService.queryObject(1);
            if(remitEntity.getRemitType()==1){
                if(StringUtils.isNullOrEmpty(xiaoxiaoUser.getSignFee()) || (xiaoxiaoUser.getSignFee().compareTo(remitEntity.getRemitFee())<0)){
                    return R.error("账户金额不足");
                }
                xiaoxiaoUser.setSignFee(xiaoxiaoUser.getSignFee().subtract(remitEntity.getRemitFee()));
                if(StringUtils.isNullOrEmpty(sysUser.getSignFee())){
                    sysUser.setSignFee(remitEntity.getRemitFee());
                }else{
                    sysUser.setSignFee(sysUser.getSignFee().add(remitEntity.getRemitFee()));
                }
            }
            if(remitEntity.getRemitType()==2){
                if(StringUtils.isNullOrEmpty(xiaoxiaoUser.getBalanceFee()) || (xiaoxiaoUser.getBalanceFee().compareTo(remitEntity.getRemitFee())<0)){
                    return R.error("账户金额不足");
                }
                xiaoxiaoUser.setBalanceFee(xiaoxiaoUser.getBalanceFee().subtract(remitEntity.getRemitFee()));
                if(StringUtils.isNullOrEmpty(sysUser.getBalanceFee())){
                    sysUser.setBalanceFee(remitEntity.getRemitFee());
                }else{
                    sysUser.setBalanceFee(sysUser.getBalanceFee().add(remitEntity.getRemitFee()));
                }
            }
            userService.update(sysUser);
            userService.update(xiaoxiaoUser);
        }
        remitEntity.setRemitStatus(3);
        remitEntity.setRemitTime(new Date());
        remitService.update(remitEntity);
        return R.ok("执行成功");
    }
}
